package com.seafood.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC配置类
 * 配置静态资源访问路径，支持本地文件访问
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Value("${app.upload.path:./uploads/}")
    private String uploadPath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将本地文件路径映射为静态资源访问路径
        // 例如：本地文件 ./uploads/product_image/2024/01/01/xxxx.jpg 
        // 可通过 http://localhost:8080/api/files/product_image/2024/01/01/xxxx.jpg 访问
        String fileUploadPath = "file:" + uploadPath.replace("\\", "/");
        if (!fileUploadPath.endsWith("/")) {
            fileUploadPath += "/";
        }
        
        registry.addResourceHandler("/files/**")
                .addResourceLocations(fileUploadPath);
    }
}